package com.parking.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parking.common.core.model.StateData;
import com.parking.platform.dao.PlatformAccountMapper;
import com.parking.platform.dao.PlatformAccountRoleMapper;
import com.parking.platform.dao.PlatformRoleMapper;
import com.parking.platform.entity.PlatformAccount;
import com.parking.platform.entity.PlatformAccountRole;
import com.parking.platform.entity.PlatformRole;
import com.parking.platform.exception.PlatformException;
import com.parking.platform.exception.PlatformExceptionCode;
import com.parking.platform.form.UpAccountRoleForm;
import com.parking.platform.service.PlatformAccountRoleService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 香菜狗带
 * @since 2023年11月28日
 */
@Service
public class PlatformAccountRoleServiceImpl extends ServiceImpl<PlatformAccountRoleMapper, PlatformAccountRole> implements PlatformAccountRoleService {

    @Resource
    private PlatformAccountRoleMapper platformAccountRoleMapper;
    @Resource
    private PlatformAccountMapper platformAccountMapper;
    @Resource
    private PlatformRoleMapper platformRoleMapper;
    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 修改平台管理员角色
     * platformId：平台管理员ID
     * platformRoleId：角色ID
     * Authorization:令牌
     * */
    @Transactional
    @Override
    public void modifyAccountRoleById(Integer platformId, UpAccountRoleForm form) {
        PlatformAccount account = platformAccountMapper.selectById(form.getPlatformId());
        if (account==null) {
            throw new PlatformException(PlatformExceptionCode.FAILED_ACCOUNT_IS_NO);
        }
        if (account.getPlatformState()!= StateData.USER_STATE_ENABLE) {
            throw new PlatformException(PlatformExceptionCode.FAILED_ROLE_STATE);
        }
        PlatformRole platformRole = platformRoleMapper.selectById(form.getPlatformRoleId());
        if (platformRole==null) {
            throw new PlatformException(PlatformExceptionCode.FAILED_ROLE_NO_EXIST);
        }
        if (platformRole.getPlatformRoleState()!= StateData.PLATFORM_ROLE_STATE_ENABLE) {
            throw new PlatformException(PlatformExceptionCode.FAILED_ROLE_STATE);
        }
        QueryWrapper<PlatformAccountRole> wrapper = new QueryWrapper<>();
        wrapper.eq("platform_account_id",form.getPlatformId());
        PlatformAccountRole role = platformAccountRoleMapper.selectOne(wrapper);
        PlatformRole platformRole2 = platformRoleMapper.selectById(role.getPlatformRoleId());
//        if (platformRole2.getPlatformRoleState()!= StateData.PLATFORM_ROLE_STATE_ENABLE) {
//            throw new PlatformException(PlatformExceptionCode.FAILED_ROLE_STATE);
//        }
        role.setPlatformRoleId(form.getPlatformRoleId());
        platformAccountRoleMapper.updateById(role);
        account.setPlatformRoleId(form.getPlatformRoleId());
        platformAccountMapper.updateById(account);
        account.setPlatformUpdatePerson(String.valueOf(platformId));
        account.setPlatformTime(LocalDateTime.now());
        HashMap<String, Object> map = new HashMap<>();
        map.put("platformAccount",account);
        rabbitTemplate.convertAndSend("upPlatformData",map);

    }
}
